package com.goddess.yeecolor.activity;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.goddess.yeecolor.R;
import com.goddess.yeecolor.base.BaseActivity;
import com.goddess.yeecolor.fragment.CollegeFragment;
import com.goddess.yeecolor.fragment.ExerciseFragment;
import com.goddess.yeecolor.fragment.MyFragment;
import com.goddess.yeecolor.tools.SharePreceUtils;

import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 主Activity
 */
@RunWith(Parameterized.class)
public class MainActivity extends BaseActivity {
    //private TextView title_content;
    //private Toolbar mToolbar;
    private CollegeFragment mCollege;
    private ExerciseFragment mExercise;
    private MyFragment my;
    private RadioGroup mRadioGroup;
    private LinearLayout mLinearlayout;
    private Bundle bundle;
    private String tag;//判断值  退出登录
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏 一些手机如果有虚拟键盘的话,虚拟键盘就会变成透明的,挡住底部按钮点击事件所以,最后不要用
            //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }

        setContentView(R.layout.activity_main);
        initFindView();
        initData();
        initOnClck();
//        setPerssion();

        if (SharePreceUtils.getInt("type","show")==1){
            SharePreceUtils.saveOne("type",-1,"show");
            showFragment(my);
            mRadioGroup.check(R.id.main_radioMy);
        }
        else if (SharePreceUtils.getInt("type","show")==3){
            showFragment(mExercise);
            mRadioGroup.check(R.id.main_radioExercise);
        }
        else {
            showFragment(mCollege);
            mRadioGroup.check(R.id.main_radioCollege);
        }
    }


    public void initOnClck() {
        mRadioGroup.setOnCheckedChangeListener(((radioGroup, i) -> {
            setCheck();
        }));

    }


    public void initFindView() {
        mLinearlayout= (LinearLayout) findViewById(R.id.allFraments);
        mRadioGroup= (RadioGroup) findViewById(R.id.main_radio);
    }


    public void initData() {
        mExercise=new ExerciseFragment();
        mCollege=new CollegeFragment();
        my=new MyFragment();

        hideFragmet();
    }


    /**
     * 选择的radiobutton 判断选择的fragment
     */
    private void setCheck(){
        int id=mRadioGroup.getCheckedRadioButtonId();
        switch (id){
            case R.id.main_radioExercise:
                showFragment(mExercise);
                mRadioGroup.check(R.id.main_radioExercise);
                break;
            case R.id.main_radioCollege:
                showFragment(mCollege);
                mRadioGroup.check(R.id.main_radioCollege);
                break;
            case R.id.main_radioMy:
                showFragment(my);
                mRadioGroup.check(R.id.main_radioMy);
                break;
        }
    }

    /**
     * 添加隐藏所有的fragment
     */
    private void hideFragmet(){

        getSupportFragmentManager().beginTransaction().add(R.id.allFraments,mExercise).commit();
        getSupportFragmentManager().beginTransaction().add(R.id.allFraments,mCollege).commit();
        getSupportFragmentManager().beginTransaction().add(R.id.allFraments,my).commit();

        getSupportFragmentManager().beginTransaction().hide(mExercise).commit();
        getSupportFragmentManager().beginTransaction().hide(mCollege).commit();
        getSupportFragmentManager().beginTransaction().hide(my).commit();

    }

    private void showFragment(Fragment fragment){
        getSupportFragmentManager().beginTransaction().hide(mExercise).commit();
        getSupportFragmentManager().beginTransaction().hide(mCollege).commit();
        getSupportFragmentManager().beginTransaction().hide(my).commit();

        getSupportFragmentManager().beginTransaction().show(fragment).commit();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==event.KEYCODE_BACK){
            exitByDoubleClick();
        }
        return false;
    }


    /**
     * 双击退出
     */
    private Timer tExit=null;
    private boolean isExit;
    private void exitByDoubleClick(){
        if (!isExit){
            isExit=true;
            Toast.makeText(MainActivity.this,"再按一次退出程序",Toast.LENGTH_SHORT).show();
            tExit=new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit=false;
                }
            },2000);
        }
        else {
            finish();
            System.exit(0);
        }
    }

    /**
     * 请求权限
     */
    private void setPerssion(){

        if (!hasPermissin(needPermissions)){
            requestPermission(4,needPermissions);
        }
        else {

            return;
        }
    }


}
